<template>
  <el-dialog :title="!dataForm.id ? '新建' :'编辑'"
             :close-on-click-modal="false" append-to-body
             :visible.sync="visible" class="JNPF-dialog JNPF-dialog_center" lock-scroll
             width="600px">
    <el-row :gutter="15" class="">
      <el-form ref="elForm" :model="dataForm" :rules="rules" size="mini" label-width="100px" label-position="top">
        <template v-if="!loading">
          <el-col :span="24">
            <el-form-item label="近照日期" prop="imageDate">
              <el-date-picker v-model="dataForm.imageDate"
                              placeholder="请选择" clearable :style='{"width":"100%"}' type="date" format="yyyy-MM-dd"
                              value-format="timestamp">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="资产近照" prop="imageUrl">
              <JNPF-UploadImg v-model="dataForm.imageUrl" :fileSize="10" sizeUnit="MB" :limit="5">
                注：
              </JNPF-UploadImg>
            </el-form-item>
          </el-col>
        </template>
      </el-form>
      <SelectDialog v-if="selectDialogVisible" :config="currTableConf" :formData="dataForm"
                    ref="selectDialog" @select="addForSelect" @close="selectDialogVisible=false"/>
    </el-row>
    <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false"> 取 消</el-button>
        <el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading"> 确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import request from '@/utils/request'
import {getDataInterfaceRes} from '@/api/systemData/dataInterface'
import {getDictionaryDataSelector} from '@/api/systemData/dictionary'

export default {
  components: {},
  props: [],
  data() {
    return {
      visible: false,
      loading: false,
      btnLoading: false,
      selectDialogVisible: false,
      currTableConf: {},
      addTableConf: {},
      tableRows: {},
      currVmodel: "",
      dataForm: {
        imageDate: '',
        imageUrl: [],
        assetId: ""
      },
      rules:
        {
          imageDate: [
            {
              required: true,
              message: '请选择',
              trigger: 'change'
            },
          ],
          imageUrl: [
            {
              required: true,
              message: '请至少选择一个',
              trigger: 'click'
            },
          ],
        },
      customerSourceOptions: [],
      customerTypeOptions: [],
      customerNatureOptions: [],
      industryOptions: [],

    }
  },
  computed: {},
  watch: {},
  created() {
  },
  mounted() {
  },
  methods: {
    clearData() {
      this.dataForm.imageDate = '';
      this.dataForm.imageUrl = [];
      this.dataForm.assetId = "";
    },
    init(record) {
      // this.dataForm.assetId = record.assetId;
      this.clearData()
      this.dataForm = Object.assign(this.dataForm, record);
      this.visible = true;
      this.$nextTick(() => {
        this.$refs['elForm'].resetFields();
      });
      this.$store.commit('generator/UPDATE_RELATION_DATA', {})
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['elForm'].validate((valid) => {
        if (valid) {
          this.request()
        }
      })
    },
    request() {
      this.btnLoading = true
      let _data = this.dataList()
      request({
        url: '/api/zc/zcAssetInfo/addImage',
        method: 'post',
        data: _data
      }).then((res) => {
        this.$message({
          message: res.msg,
          type: 'success',
          duration: 1000,
          onClose: () => {
            this.visible = false
            this.btnLoading = false
            this.$emit('refresh', true)
          }
        })
      }).catch(() => {
        this.btnLoading = false
      })
    },
    openSelectDialog(key) {
      this.currTableConf = this.addTableConf[key]
      this.currVmodel = key
      this.selectDialogVisible = true
      this.$nextTick(() => {
        this.$refs.selectDialog.init()
      })
    },
    addForSelect(data) {
      for (let i = 0; i < data.length; i++) {
        let item = {...this.tableRows[this.currVmodel], ...data[i]}
        this.dataForm[this.currVmodel].push(item)
      }
    },
    dataList() {
      var _data = JSON.parse(JSON.stringify(this.dataForm));
      _data.imageUrl = JSON.stringify(_data.imageUrl)
      return _data;
    },
    dataInfo(dataAll) {
      let _dataAll = dataAll
      _dataAll.imageUrl = JSON.parse(_dataAll.imageUrl)
      this.dataForm = _dataAll
    },
  },
}

</script>
